{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "NumPy 提供了对多维数组（矩阵）的高效支持\n",
    "1. Ndarray, 核心数据结构，支持矢量运算的多维数组，在内存中进行连续存储。\n",
    "2. 各种操作多维数组的函数。\n",
    "3. 用于集成其他编程语言的各种接口。"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "497c8894107a6c08"
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:27:56.977950700Z",
     "start_time": "2025-01-07T12:27:56.917654Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([1, 2, 3, 4])"
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 多维的创建\n",
    "import numpy as np\n",
    "\n",
    "a1 = np.array([1,2,3,4])\n",
    "a1"
   ]
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(4,)"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:28:20.119588300Z",
     "start_time": "2025-01-07T12:28:20.106414Z"
    }
   },
   "id": "a28b6795cbfb99cd",
   "execution_count": 2
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "dtype('int64')"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1.dtype"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:28:29.798564600Z",
     "start_time": "2025-01-07T12:28:29.774299800Z"
    }
   },
   "id": "d040d414ec071f2d",
   "execution_count": 3
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([[1. , 2.5, 3. ],\n       [0.5, 4. , 9. ]])"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2 = np.array([[1.0, 2.5, 3], [0.5, 4, 9]])\n",
    "a2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:28:39.638874700Z",
     "start_time": "2025-01-07T12:28:39.625703700Z"
    }
   },
   "id": "dad5c2361e6312f9",
   "execution_count": 4
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(2, 3)"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:29:06.148507200Z",
     "start_time": "2025-01-07T12:29:06.135315700Z"
    }
   },
   "id": "57fe67a3e0487ba7",
   "execution_count": 5
  },
  {
   "cell_type": "markdown",
   "source": [
    "ndarray.size 属性得到数组的元素个数\n",
    "ndarray.dtype 属性则记录了数组内部存储的元素是什么类型，"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "83cdb7fe89001b90"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "6"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2.size"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:29:16.158697700Z",
     "start_time": "2025-01-07T12:29:16.144865500Z"
    }
   },
   "id": "43b7f2f2d6396d85",
   "execution_count": 6
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "np.float64(0.5)"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2.min()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:29:21.473397100Z",
     "start_time": "2025-01-07T12:29:21.460538300Z"
    }
   },
   "id": "af98e09e785aa4f3",
   "execution_count": 7
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "dtype('float64')"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2.dtype"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:29:26.608725Z",
     "start_time": "2025-01-07T12:29:26.595180500Z"
    }
   },
   "id": "b89579a0c18ff142",
   "execution_count": 8
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "numpy.ndarray"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(a1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:29:40.080405700Z",
     "start_time": "2025-01-07T12:29:40.069306700Z"
    }
   },
   "id": "3bb8084c2797c853",
   "execution_count": 9
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "numpy.ndarray"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(a2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:29:44.271151200Z",
     "start_time": "2025-01-07T12:29:44.257208600Z"
    }
   },
   "id": "ec4f2a07f574d5c6",
   "execution_count": 10
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 创建方法\n",
    "1. np.array 直接创建数组；\n",
    "2. np.arange 类似于 Python 内置的 range，创建一维数组；\n",
    "3. np.ones 创建元素值全部为 1 的数组；\n",
    "4. np.zeros 创建元素值全为 0 的数组；\n",
    "5. np.empty 创建空值多维数组，只分配内存，不填充任何值；\n",
    "6. np.random.random 创建元素值为随机值的多维数组；"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "d8eb2593f5b02057"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([0, 1, 2, 3])"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1 = np.arange(4)\n",
    "a1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:33:13.248783600Z",
     "start_time": "2025-01-07T12:33:13.235361800Z"
    }
   },
   "id": "5d713fa7ee9b6c45",
   "execution_count": 11
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "1"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1.ndim"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:33:23.749945500Z",
     "start_time": "2025-01-07T12:33:23.734546Z"
    }
   },
   "id": "2d8f3960c73ba1e6",
   "execution_count": 12
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([[1, 1, 1, 1],\n       [1, 1, 1, 1],\n       [1, 1, 1, 1],\n       [1, 1, 1, 1]])"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2 = np.ones((4, 4), dtype=np.int64)\n",
    "a2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:33:36.955368800Z",
     "start_time": "2025-01-07T12:33:36.943252500Z"
    }
   },
   "id": "f70e19e707f239f6",
   "execution_count": 13
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([[1., 1., 1., 1.],\n       [1., 1., 1., 1.],\n       [1., 1., 1., 1.],\n       [1., 1., 1., 1.]])"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a3 = np.ones((4, 4))\n",
    "a3"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:33:56.759567200Z",
     "start_time": "2025-01-07T12:33:56.744199700Z"
    }
   },
   "id": "fe79d609d21f84b6",
   "execution_count": 14
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "dtype('int64')"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2.dtype"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:34:09.643492200Z",
     "start_time": "2025-01-07T12:34:09.630181800Z"
    }
   },
   "id": "90f614f1dcc86ee",
   "execution_count": 15
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "2"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2.ndim"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:34:14.512272500Z",
     "start_time": "2025-01-07T12:34:14.500034500Z"
    }
   },
   "id": "22c31dd50ce4e476",
   "execution_count": 16
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(4, 4)"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:34:32.594874200Z",
     "start_time": "2025-01-07T12:34:32.578499300Z"
    }
   },
   "id": "a30b49fc6939356",
   "execution_count": 17
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([[0., 0.],\n       [0., 0.]])"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a4 = np.zeros((2, 2))\n",
    "a4"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:34:43.028607100Z",
     "start_time": "2025-01-07T12:34:43.016090700Z"
    }
   },
   "id": "75c32eb479f13f69",
   "execution_count": 18
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "2"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a3.ndim"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:34:51.455250800Z",
     "start_time": "2025-01-07T12:34:51.439760900Z"
    }
   },
   "id": "493d7ab5c43fb9ba",
   "execution_count": 19
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([[206158430240, 206158430254, 206158430256],\n       [206158430256, 137438953520, 197568495664],\n       [206158430256, 206158430256, 206158430256]])"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a5 = np.empty((3, 3), dtype=np.int64)\n",
    "a5"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:35:02.423270700Z",
     "start_time": "2025-01-07T12:35:02.409545800Z"
    }
   },
   "id": "65130841cb1bc146",
   "execution_count": 20
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([[[1., 1., 1., 1.],\n        [1., 1., 1., 1.],\n        [1., 1., 1., 1.]],\n\n       [[1., 1., 1., 1.],\n        [1., 1., 1., 1.],\n        [1., 1., 1., 1.]],\n\n       [[1., 1., 1., 1.],\n        [1., 1., 1., 1.],\n        [1., 1., 1., 1.]],\n\n       [[1., 1., 1., 1.],\n        [1., 1., 1., 1.],\n        [1., 1., 1., 1.]]])"
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a6 = np.ones((4, 3, 4))\n",
    "a6"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:35:32.565750600Z",
     "start_time": "2025-01-07T12:35:32.549582100Z"
    }
   },
   "id": "e934bb2cd29a7f6f",
   "execution_count": 21
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "3"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a6.ndim"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:35:54.121467100Z",
     "start_time": "2025-01-07T12:35:54.108442200Z"
    }
   },
   "id": "6bd71554fc0dbee9",
   "execution_count": 22
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])"
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a7 = np.arange(12)\n",
    "a7"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:36:13.850619100Z",
     "start_time": "2025-01-07T12:36:13.836695300Z"
    }
   },
   "id": "dffdda13f678e520",
   "execution_count": 23
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 0,  1,  2],\n       [ 3,  4,  5],\n       [ 6,  7,  8],\n       [ 9, 10, 11]])"
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# reshape 方法的参数为指定数组形状的元组\n",
    "a7.reshape(4, 3)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:36:20.983162500Z",
     "start_time": "2025-01-07T12:36:20.966633800Z"
    }
   },
   "id": "8785bdd3e96f7119",
   "execution_count": 24
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 多维数组索引"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "d49ed73dd01ed771"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "[1, 2, 3, 4, 5]"
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l = [1, 2, 3, 4, 5]\n",
    "l"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:37:14.674883Z",
     "start_time": "2025-01-07T12:37:14.661058900Z"
    }
   },
   "id": "be32ff37228ca5d5",
   "execution_count": 25
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "[1, 2]"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l[:2]   # 选择列表中索引为 2 前面的所有元素"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:37:20.518393300Z",
     "start_time": "2025-01-07T12:37:20.502220700Z"
    }
   },
   "id": "cf01cafcadf5bd71",
   "execution_count": 26
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "[3, 4]"
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l[2:4]  # 选择列表中索引为 2 到索引为 3 的元素"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:37:28.060627800Z",
     "start_time": "2025-01-07T12:37:28.046382Z"
    }
   },
   "id": "8fe0e5343c8a63ae",
   "execution_count": 27
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "[2, 4]"
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l[1:5:2]    # 选择列表中的索引为 1 到索引为 4，且步长为 2 的元素"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:37:45.384138800Z",
     "start_time": "2025-01-07T12:37:45.366848300Z"
    }
   },
   "id": "93974e7a7c335bb4",
   "execution_count": 28
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])"
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(12)\n",
    "a"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:38:41.305385300Z",
     "start_time": "2025-01-07T12:38:41.290834600Z"
    }
   },
   "id": "d5e82a9d555592a3",
   "execution_count": 29
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([1, 2, 3])"
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[1:4]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:38:54.381480400Z",
     "start_time": "2025-01-07T12:38:54.360525200Z"
    }
   },
   "id": "5c5cb50fc2fb889a",
   "execution_count": 30
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([1, 3, 5, 7, 9])"
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[1:10:2]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:39:00.471915600Z",
     "start_time": "2025-01-07T12:39:00.458593100Z"
    }
   },
   "id": "b29e0cec9050b40b",
   "execution_count": 31
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 0, -1, -1, -1, -1,  5,  6,  7,  8,  9, 10, 11])"
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[1:5] = -1\n",
    "a"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:39:27.383722600Z",
     "start_time": "2025-01-07T12:39:27.359741Z"
    }
   },
   "id": "f4e448fa687ce08b",
   "execution_count": 33
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 0,  1,  2,  3],\n       [ 4,  5,  6,  7],\n       [ 8,  9, 10, 11]])"
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(12).reshape(3, 4)\n",
    "a"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:39:42.390701400Z",
     "start_time": "2025-01-07T12:39:42.373804800Z"
    }
   },
   "id": "39551ac8814a5b92",
   "execution_count": 34
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(3, 4)"
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:39:53.052887900Z",
     "start_time": "2025-01-07T12:39:53.035157900Z"
    }
   },
   "id": "45b606924a017191",
   "execution_count": 35
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([0, 1, 2, 3])"
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:39:58.537843600Z",
     "start_time": "2025-01-07T12:39:58.503459800Z"
    }
   },
   "id": "6cf53e01559c543e",
   "execution_count": 36
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([4, 5, 6, 7])"
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[1]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:40:06.370079Z",
     "start_time": "2025-01-07T12:40:06.352606200Z"
    }
   },
   "id": "bad522bd51155d02",
   "execution_count": 37
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([0, 4, 8])"
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[:, 0]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:40:20.074910400Z",
     "start_time": "2025-01-07T12:40:20.055913Z"
    }
   },
   "id": "84629dd53c17e81",
   "execution_count": 38
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([1, 5, 9])"
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[:, 1]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:40:35.661700100Z",
     "start_time": "2025-01-07T12:40:35.643505600Z"
    }
   },
   "id": "ea2d2170d52cef64",
   "execution_count": 39
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "np.int64(0)"
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0, 0]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:40:42.333182400Z",
     "start_time": "2025-01-07T12:40:42.312503100Z"
    }
   },
   "id": "d0b0f32a7db018cb",
   "execution_count": 40
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "np.int64(6)"
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[1, 2]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:40:48.601736800Z",
     "start_time": "2025-01-07T12:40:48.581450600Z"
    }
   },
   "id": "a89e610a9108e17f",
   "execution_count": 41
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([[[ 0,  1,  2],\n        [ 3,  4,  5],\n        [ 6,  7,  8]],\n\n       [[ 9, 10, 11],\n        [12, 13, 14],\n        [15, 16, 17]],\n\n       [[18, 19, 20],\n        [21, 22, 23],\n        [24, 25, 26]]])"
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(27).reshape(3, 3, 3)\n",
    "a"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:41:12.646337Z",
     "start_time": "2025-01-07T12:41:12.631442700Z"
    }
   },
   "id": "80656cc0884dde4a",
   "execution_count": 42
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "3"
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.ndim"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:41:20.836702200Z",
     "start_time": "2025-01-07T12:41:20.802495200Z"
    }
   },
   "id": "74433461b1b3ef2e",
   "execution_count": 43
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(3, 3, 3)"
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:41:25.044369200Z",
     "start_time": "2025-01-07T12:41:25.030401400Z"
    }
   },
   "id": "e2c1de08a3f93655",
   "execution_count": 44
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 9, 10, 11],\n       [12, 13, 14],\n       [15, 16, 17]])"
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1 = a[1]\n",
    "a1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:41:31.329320100Z",
     "start_time": "2025-01-07T12:41:31.316544100Z"
    }
   },
   "id": "adcbceef01799897",
   "execution_count": 45
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(3, 3)"
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:41:43.948036200Z",
     "start_time": "2025-01-07T12:41:43.933104700Z"
    }
   },
   "id": "1be6b3b96271c56c",
   "execution_count": 46
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([12, 13, 14])"
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2 = a[1, 1]\n",
    "a2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:41:59.205207600Z",
     "start_time": "2025-01-07T12:41:59.178142200Z"
    }
   },
   "id": "56dabfedf7e029f3",
   "execution_count": 47
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(3,)"
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:42:14.775960100Z",
     "start_time": "2025-01-07T12:42:14.745637800Z"
    }
   },
   "id": "292fc46ed0c7c2ac",
   "execution_count": 48
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "1"
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2.ndim"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:42:18.966550900Z",
     "start_time": "2025-01-07T12:42:18.927767700Z"
    }
   },
   "id": "ec5f9f0ff9be0965",
   "execution_count": 49
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 多维数组基础运算"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "a8a12db95cbf2dfd"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 0,  1,  2,  3],\n       [ 4,  5,  6,  7],\n       [ 8,  9, 10, 11]])"
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(12).reshape(3, 4)\n",
    "a"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:42:35.235184500Z",
     "start_time": "2025-01-07T12:42:35.211631Z"
    }
   },
   "id": "c1df34b528de24f",
   "execution_count": 50
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 1,  2,  3,  4],\n       [ 5,  6,  7,  8],\n       [ 9, 10, 11, 12]])"
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a += 1\n",
    "a"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:42:39.737939800Z",
     "start_time": "2025-01-07T12:42:39.701684800Z"
    }
   },
   "id": "91b388576fa80e42",
   "execution_count": 51
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 1,  2,  3,  4],\n       [ 5,  6,  7,  8],\n       [ 9, 10, 11, 12]])"
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(1,13).reshape(3, 4)\n",
    "a"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:43:01.208855500Z",
     "start_time": "2025-01-07T12:43:01.187526700Z"
    }
   },
   "id": "f3d449249afd6797",
   "execution_count": 52
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 2,  4,  6,  8],\n       [10, 12, 14, 16],\n       [18, 20, 22, 24]])"
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a *= 2\n",
    "a"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:43:08.838181Z",
     "start_time": "2025-01-07T12:43:08.812754100Z"
    }
   },
   "id": "ffca966f3c560533",
   "execution_count": 53
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([[0, 1],\n       [2, 3]])"
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(4).reshape(2, 2)\n",
    "a"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:44:09.722562600Z",
     "start_time": "2025-01-07T12:44:09.698540Z"
    }
   },
   "id": "93b83c188820ebb9",
   "execution_count": 58
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([[4, 5],\n       [6, 7]])"
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.arange(4, 8).reshape(2, 2)\n",
    "b"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:44:19.530462700Z",
     "start_time": "2025-01-07T12:44:19.487726100Z"
    }
   },
   "id": "cbe33d184bbfcc7e",
   "execution_count": 59
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([[4, 4],\n       [4, 4]])"
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b - a"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:44:24.016047300Z",
     "start_time": "2025-01-07T12:44:23.976311300Z"
    }
   },
   "id": "98cb84be10c6f4ae",
   "execution_count": 60
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 4,  6],\n       [ 8, 10]])"
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a + b"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:44:30.656366Z",
     "start_time": "2025-01-07T12:44:30.630630800Z"
    }
   },
   "id": "3f3bb188be9c2dbb",
   "execution_count": 61
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 0,  5],\n       [12, 21]])"
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a * b"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:44:40.727902200Z",
     "start_time": "2025-01-07T12:44:40.690202100Z"
    }
   },
   "id": "42cabd80c194c403",
   "execution_count": 62
  },
  {
   "cell_type": "markdown",
   "source": [
    "多维数组间的运算规则是相同位置（坐标）上的值进行运算得到最终结果\n",
    "\n",
    "多维数组的组织方式和矩阵相同，但其乘法的运算规则却和矩阵的运算规则不同\n",
    "如果想对 Ndarray 对象使用矩阵的乘法运算规则，可以使用 ndarray.dot 方法"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "1a28c65e6dc43960"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 6,  7],\n       [26, 31]])"
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.dot(b)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:45:22.232858400Z",
     "start_time": "2025-01-07T12:45:22.210589900Z"
    }
   },
   "id": "ca14f1b94ac8a1aa",
   "execution_count": 63
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([[False, False, False],\n       [False, False, False],\n       [ True,  True,  True],\n       [ True,  True,  True]])"
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(12).reshape(4, 3)\n",
    "b = a > 5\n",
    "b"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:45:44.012967900Z",
     "start_time": "2025-01-07T12:45:43.985969500Z"
    }
   },
   "id": "65fc7ff1a79bf61b",
   "execution_count": 64
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 6,  7,  8,  9, 10, 11])"
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[b]    # 使用 a[b] 这种形式列出 a 中所有大于 5 的元素"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:45:55.526403900Z",
     "start_time": "2025-01-07T12:45:55.501714400Z"
    }
   },
   "id": "3a49e99540f20e5d",
   "execution_count": 65
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 统计数组中的一些统计量\n",
    "a.sum 计算多维数组的所有元素的和；\n",
    "a.max 最大值计算；\n",
    "a.min 最小值计算；\n",
    "a.mean 平均值计算；\n",
    "a.std 标准差计算；\n",
    "a.var 方差计算；\n",
    "\n",
    "以上所有方法，都可以接受一个 axis 参数，用于指定具体统计哪根轴上的数据。\n",
    "比如二维数组，\n",
    "可以理解为有 x, y 两根轴，分别代表行和列，\n",
    "指定 axis=0 时代表分别统计每列上的数据，axis=1 时，代表分别统计每一行上的数据。\n",
    "没有指定axis 参数时，代表统计所有元素。"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "2c7b287d6e7d493e"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 0,  1,  2],\n       [ 3,  4,  5],\n       [ 6,  7,  8],\n       [ 9, 10, 11]])"
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:47:55.195968400Z",
     "start_time": "2025-01-07T12:47:55.181682200Z"
    }
   },
   "id": "874ce9a5442b0e1f",
   "execution_count": 69
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "np.int64(66)"
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:47:42.601726200Z",
     "start_time": "2025-01-07T12:47:42.573885300Z"
    }
   },
   "id": "39915c52e1808949",
   "execution_count": 67
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([18, 22, 26])"
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.sum(axis=0)   # 每列和"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:47:50.288691400Z",
     "start_time": "2025-01-07T12:47:50.266399400Z"
    }
   },
   "id": "76975b11c4879c99",
   "execution_count": 68
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 3, 12, 21, 30])"
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.sum(axis=1)   # 每行和"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:48:28.741698700Z",
     "start_time": "2025-01-07T12:48:28.714436700Z"
    }
   },
   "id": "547372e3c03e3e86",
   "execution_count": 70
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 通用函数\n",
    "np.sqrt 开方运算；\n",
    "np.dot 矩阵乘法；\n",
    "np.sort 排序；\n",
    "np.linalg 模块中包含了一些基本的线性代数计算函数；"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "f3931ebc37b72dda"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Pandas 数据处理包\n",
    "Pandas 处理二维数据时更加得心应手\n",
    "Series 和 DataFrame是 Pandas 中的两种核心数据结构"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "862ab8e802ef3e73"
  },
  {
   "cell_type": "markdown",
   "source": [
    "Series 是值的序列，可以理解为一维数组，它只有一个列和索引。\n",
    "索引可以定制，当不指定时默认使用整数索引，而且索引可以被命名"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "fdd107a9f24a962f"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "0    1\n1    2\n2    3\n3    4\n4    5\ndtype: int64"
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "s1 = pd.Series([1, 2, 3, 4, 5])\n",
    "s1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T13:02:38.133584500Z",
     "start_time": "2025-01-07T13:02:38.111921900Z"
    }
   },
   "id": "c4bf9e3c9accf458",
   "execution_count": 73
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "a    1\nb    2\nc    3\nd    4\ne    5\ndtype: int64"
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2 = pd.Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 'd', 'e'])\n",
    "s2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T13:03:10.950042400Z",
     "start_time": "2025-01-07T13:03:10.919969600Z"
    }
   },
   "id": "7328f514cf7b3133",
   "execution_count": 75
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "index\na    1\nb    2\nc    3\nd    4\ne    5\ndtype: int64"
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2.index.name = 'index'\n",
    "s2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T13:03:32.196923600Z",
     "start_time": "2025-01-07T13:03:32.169002300Z"
    }
   },
   "id": "3c9535724518013e",
   "execution_count": 77
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "Index(['a', 'b', 'c', 'd', 'e'], dtype='object', name='index')"
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2.index"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T13:03:41.016901500Z",
     "start_time": "2025-01-07T13:03:40.970452600Z"
    }
   },
   "id": "3395c6ad3701d05d",
   "execution_count": 78
  },
  {
   "cell_type": "markdown",
   "source": [
    "DataFrame 类似于二维数组，有行和列之分，除了像 Series 一样，多个行有索引而外，\n",
    "每个列上面还可以有标签 label, 索引和标签本身都可以被命名：\n",
    "\n",
    "通过指定索引和标签（columns 参数）创建了一个 DataFrame 实例。\n",
    "可以通过 df.index 和 df.columns 分别访问索引和标签"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "6e0a25f9c05e9359"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "          A         B         C         D\na -0.790722  0.687331  1.239368  1.063530\nb -0.634574  0.759545 -0.592672  0.675267\nc  0.682011 -1.442541  1.038549  1.157584\nd -1.416570  0.764000  0.418996 -0.286263",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>-0.790722</td>\n      <td>0.687331</td>\n      <td>1.239368</td>\n      <td>1.063530</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>-0.634574</td>\n      <td>0.759545</td>\n      <td>-0.592672</td>\n      <td>0.675267</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>0.682011</td>\n      <td>-1.442541</td>\n      <td>1.038549</td>\n      <td>1.157584</td>\n    </tr>\n    <tr>\n      <th>d</th>\n      <td>-1.416570</td>\n      <td>0.764000</td>\n      <td>0.418996</td>\n      <td>-0.286263</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(np.random.randn(4, 4), index=[\n",
    "               'a', 'b', 'c', 'd'], columns=['A', 'B', 'C', 'D'])\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T13:04:34.036351500Z",
     "start_time": "2025-01-07T13:04:34.005947400Z"
    }
   },
   "id": "505eac333bf016cd",
   "execution_count": 79
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "Index(['a', 'b', 'c', 'd'], dtype='object')"
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.index"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T13:04:49.607297100Z",
     "start_time": "2025-01-07T13:04:49.560561800Z"
    }
   },
   "id": "4949a93291733825",
   "execution_count": 80
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "Index(['A', 'B', 'C', 'D'], dtype='object')"
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.columns"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T13:05:01.942386900Z",
     "start_time": "2025-01-07T13:05:01.899345600Z"
    }
   },
   "id": "908b93e49df8b407",
   "execution_count": 81
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 选择\n",
    "类似于 NumPy 数组，当生成了 Series 和 DataFrame 数据时，我们怎么选择其中的部分数据呢？\n",
    "\n",
    "对于 Series 数据来说，主要通过其索引来进行选择"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "bbee4f6d802f2a64"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "a    1\nb    2\nc    3\nd    4\ne    5\ndtype: int64"
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2 = pd.Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 'd', 'e'])\n",
    "s2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T13:05:56.725328200Z",
     "start_time": "2025-01-07T13:05:56.704131500Z"
    }
   },
   "id": "ee655c9170b8100e",
   "execution_count": 82
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\bzj27\\AppData\\Local\\Temp\\ipykernel_73588\\2953510796.py:1: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  s2[0]\n"
     ]
    },
    {
     "data": {
      "text/plain": "np.int64(1)"
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2[0]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T13:06:29.670167300Z",
     "start_time": "2025-01-07T13:06:29.655778Z"
    }
   },
   "id": "e1ece97982bf0933",
   "execution_count": 84
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "np.int64(1)"
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2['a']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T13:06:36.371546500Z",
     "start_time": "2025-01-07T13:06:36.333126700Z"
    }
   },
   "id": "bf63f7399fbf73bf",
   "execution_count": 85
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "a    1\nb    2\nc    3\ndtype: int64"
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2[0:3]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T13:06:46.633727100Z",
     "start_time": "2025-01-07T13:06:46.585356500Z"
    }
   },
   "id": "d6e638811ca4a47f",
   "execution_count": 86
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "a    1\nb    2\nc    3\ndtype: int64"
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2['a':'c']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T13:06:58.974379900Z",
     "start_time": "2025-01-07T13:06:58.949452100Z"
    }
   },
   "id": "9c4c373cb8ab73b9",
   "execution_count": 87
  },
  {
   "cell_type": "markdown",
   "source": [
    "可以看到对于指定了索引的 Series 序列来说，我们有两种选择元素的方式，\n",
    "一种是以整数索引（这说明整数索引一直默认存在），\n",
    "第二种方式是通过指定的字符索引进行。\n",
    "\n",
    "其实整数索引和字符索引，分别调用了 s2.iloc 和 s2.loc 索引，其中 iloc 代表整数索引，如下代码："
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "9977a69026508590"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "a    1\nb    2\nc    3\ndtype: int64"
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2.iloc[0:3]    # iloc 代表整数索引"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T13:07:43.641492100Z",
     "start_time": "2025-01-07T13:07:43.609665200Z"
    }
   },
   "id": "6594a67f9adc2810",
   "execution_count": 88
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "a    1\nb    2\nc    3\ndtype: int64"
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2.loc['a':'c']   "
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T13:08:34.096730200Z",
     "start_time": "2025-01-07T13:08:34.066566300Z"
    }
   },
   "id": "d2a8ca5e74d858f0",
   "execution_count": 90
  },
  {
   "cell_type": "markdown",
   "source": [
    "对于 DataFrame 数据来说，由于有行列之分，所以可以有多种选择数据的方式，\n",
    "比如通过索引或者标签来选择，先看看对于标签（列）怎么选择："
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "cb490239f1d25722"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "          A         B         C         D\na -0.790722  0.687331  1.239368  1.063530\nb -0.634574  0.759545 -0.592672  0.675267\nc  0.682011 -1.442541  1.038549  1.157584\nd -1.416570  0.764000  0.418996 -0.286263",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>-0.790722</td>\n      <td>0.687331</td>\n      <td>1.239368</td>\n      <td>1.063530</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>-0.634574</td>\n      <td>0.759545</td>\n      <td>-0.592672</td>\n      <td>0.675267</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>0.682011</td>\n      <td>-1.442541</td>\n      <td>1.038549</td>\n      <td>1.157584</td>\n    </tr>\n    <tr>\n      <th>d</th>\n      <td>-1.416570</td>\n      <td>0.764000</td>\n      <td>0.418996</td>\n      <td>-0.286263</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T13:08:59.138506600Z",
     "start_time": "2025-01-07T13:08:59.108819300Z"
    }
   },
   "id": "f1a1d3cb92a68c13",
   "execution_count": 91
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "a   -0.790722\nb   -0.634574\nc    0.682011\nd   -1.416570\nName: A, dtype: float64"
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.A"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T13:09:17.545097900Z",
     "start_time": "2025-01-07T13:09:17.488432600Z"
    }
   },
   "id": "dc4f3481af33d2ec",
   "execution_count": 92
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "a   -0.790722\nb   -0.634574\nc    0.682011\nd   -1.416570\nName: A, dtype: float64"
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['A']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T13:09:40.369471200Z",
     "start_time": "2025-01-07T13:09:40.335364100Z"
    }
   },
   "id": "e2429ca213563e94",
   "execution_count": 94
  },
  {
   "cell_type": "markdown",
   "source": [
    "对于标签（列）选择来说，\n",
    "可以直接通过访问 df.A 的属性方式，\n",
    "或者通过 df['A'] 的方式来选择某一列，\n",
    "\n",
    "如果要选择多列怎么操作呢？\n",
    "前面我们说过，可以通过 df.columns 获取到所有的列标签，\n",
    "然后可以根据选择部分标签来选择 df 的多列数据："
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "9b0af6a693235e2f"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "Index(['A', 'B'], dtype='object')"
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.columns[0:2]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T13:10:46.217936500Z",
     "start_time": "2025-01-07T13:10:46.185741100Z"
    }
   },
   "id": "4cc91c3320125a1e",
   "execution_count": 96
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "          A         B\na -0.790722  0.687331\nb -0.634574  0.759545\nc  0.682011 -1.442541\nd -1.416570  0.764000",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>-0.790722</td>\n      <td>0.687331</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>-0.634574</td>\n      <td>0.759545</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>0.682011</td>\n      <td>-1.442541</td>\n    </tr>\n    <tr>\n      <th>d</th>\n      <td>-1.416570</td>\n      <td>0.764000</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[df.columns[0:2]]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T13:10:22.910851400Z",
     "start_time": "2025-01-07T13:10:22.880685300Z"
    }
   },
   "id": "c061dc0a37d77513",
   "execution_count": 95
  },
  {
   "cell_type": "markdown",
   "source": [
    "当然还有其他方法来选择多列数据，\n",
    "现在先看下如何来选择 df 的某行，\n",
    "\n",
    "前面我们提到过可以根据 loc 属性来选择某一行，\n",
    "iloc 根据整数索引来选择某一行，\n",
    "这些在 DataFrame 中都是可以的："
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "f5508a64c2dd4c72"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "A   -0.790722\nB    0.687331\nC    1.239368\nD    1.063530\nName: a, dtype: float64"
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc['a']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T13:12:21.852111600Z",
     "start_time": "2025-01-07T13:12:21.812664800Z"
    }
   },
   "id": "d745b514d71e1f57",
   "execution_count": 100
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "          A         B         C         D\na -0.790722  0.687331  1.239368  1.063530\nb -0.634574  0.759545 -0.592672  0.675267",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>-0.790722</td>\n      <td>0.687331</td>\n      <td>1.239368</td>\n      <td>1.063530</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>-0.634574</td>\n      <td>0.759545</td>\n      <td>-0.592672</td>\n      <td>0.675267</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc['a':'b']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T13:12:26.842903400Z",
     "start_time": "2025-01-07T13:12:26.796108600Z"
    }
   },
   "id": "3cb2ecf785799280",
   "execution_count": 101
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "A   -0.790722\nB    0.687331\nC    1.239368\nD    1.063530\nName: a, dtype: float64"
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iloc[0]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T13:12:36.887109Z",
     "start_time": "2025-01-07T13:12:36.829913100Z"
    }
   },
   "id": "ff6d105c6a1039b7",
   "execution_count": 102
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "          B         C         D\na  0.687331  1.239368  1.063530\nb  0.759545 -0.592672  0.675267\nc -1.442541  1.038549  1.157584\nd  0.764000  0.418996 -0.286263",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>0.687331</td>\n      <td>1.239368</td>\n      <td>1.063530</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>0.759545</td>\n      <td>-0.592672</td>\n      <td>0.675267</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>-1.442541</td>\n      <td>1.038549</td>\n      <td>1.157584</td>\n    </tr>\n    <tr>\n      <th>d</th>\n      <td>0.764000</td>\n      <td>0.418996</td>\n      <td>-0.286263</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[:, ['B', 'C', 'D']]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T13:12:45.775313700Z",
     "start_time": "2025-01-07T13:12:45.710842800Z"
    }
   },
   "id": "2113034593080f73",
   "execution_count": 103
  },
  {
   "cell_type": "markdown",
   "source": [
    "df.loc 支持二维的选择，也就是同时选择行和列， : 符号代表选择所有的行"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "b92cd54a4541e321"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "np.float64(-0.790722084586431)"
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc['a', 'A']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T13:13:10.629894700Z",
     "start_time": "2025-01-07T13:13:10.596557100Z"
    }
   },
   "id": "8b667e8c31ab8b8d",
   "execution_count": 104
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "          B         C\nb  0.759545 -0.592672\nc -1.442541  1.038549",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>B</th>\n      <th>C</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>b</th>\n      <td>0.759545</td>\n      <td>-0.592672</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>-1.442541</td>\n      <td>1.038549</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc['b':'c', 'B':'C']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T13:13:15.758606400Z",
     "start_time": "2025-01-07T13:13:15.689436400Z"
    }
   },
   "id": "14f546eafeb5ff61",
   "execution_count": 105
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 缺失值和数据自动对齐\n",
    "\n",
    "在 Pandas 中最重要的一个功能是，它可以对不同索引的对象进行算术运算。\n",
    "比如将两个 Series 数据进行相加时，如果存在不同的索引，则结果是两个索引的并集，什么意思呢？通过例子看下："
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "f8d7c6b999c9a755"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "a    1\nb    2\nc    3\nd    4\ndtype: int64"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "s1 = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])\n",
    "s1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T11:51:02.337390100Z",
     "start_time": "2025-01-08T11:51:01.645299600Z"
    }
   },
   "id": "c7a007f5763d4a06",
   "execution_count": 2
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "b    2\nc    3\nd    4\ne    5\ndtype: int64"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2 = pd.Series([2, 3, 4, 5], index=['b', 'c', 'd', 'e'])\n",
    "s2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T11:51:11.925202600Z",
     "start_time": "2025-01-08T11:51:11.875914400Z"
    }
   },
   "id": "b0eb8d6c3a6870d4",
   "execution_count": 3
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "a    NaN\nb    4.0\nc    6.0\nd    8.0\ne    NaN\ndtype: float64"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1 + s2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T11:51:25.071800700Z",
     "start_time": "2025-01-08T11:51:25.027198800Z"
    }
   },
   "id": "277b627a4428daf0",
   "execution_count": 4
  },
  {
   "cell_type": "markdown",
   "source": [
    "两者具有相同的索引 ['b', 'c', 'd'], 所以在进行相加时，相同索引上的值会相加，\n",
    "但不重叠的索引引入 NaN 值，也就是缺失值。\n",
    "而缺失值会在运算中传播，所以最终结果也是 NaN 值。\n",
    "根据相同的索引进行自动计算，这就是自动对齐功能。\n",
    "\n",
    "同样的规则，在 DataFrame 数据中也生效："
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "4ce5c5ddce9fc6c2"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   A  B  C\na  0  1  2\nb  3  4  5\nc  6  7  8",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>3</td>\n      <td>4</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>6</td>\n      <td>7</td>\n      <td>8</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "df1 = pd.DataFrame(np.arange(9).reshape(3, 3), \n",
    "                   columns=list('ABC'), index=list('abc'))\n",
    "df1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T11:55:57.096375600Z",
     "start_time": "2025-01-08T11:55:57.039515700Z"
    }
   },
   "id": "d6def06a1df3e0a",
   "execution_count": 10
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   A  B   D   E\nb  0  1   2   3\nc  4  5   6   7\nd  8  9  10  11",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>D</th>\n      <th>E</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>b</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>4</td>\n      <td>5</td>\n      <td>6</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>d</th>\n      <td>8</td>\n      <td>9</td>\n      <td>10</td>\n      <td>11</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = pd.DataFrame(np.arange(12).reshape(3, 4), \n",
    "                   columns=list('ABDE'), index=list('bcd'))\n",
    "df2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T11:56:55.094291200Z",
     "start_time": "2025-01-08T11:56:55.051393200Z"
    }
   },
   "id": "c77fe66d81d30a1e",
   "execution_count": 11
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "      A     B   C   D   E\na   NaN   NaN NaN NaN NaN\nb   3.0   5.0 NaN NaN NaN\nc  10.0  12.0 NaN NaN NaN\nd   NaN   NaN NaN NaN NaN",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n      <th>E</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>3.0</td>\n      <td>5.0</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>10.0</td>\n      <td>12.0</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>d</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 + df2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T11:57:06.261540Z",
     "start_time": "2025-01-08T11:57:06.227802600Z"
    }
   },
   "id": "75d3aceb7f713d42",
   "execution_count": 12
  },
  {
   "cell_type": "markdown",
   "source": [
    "可以看到，DataFrame 的计算也会进行自动对齐操作，\n",
    "这个时候没有的行或者列会使用 NaN 值自动填充，\n",
    "而由于 NaN 值会传播，所以相加的结果也是 NaN。\n",
    "\n",
    "当然我们在计算时，可以指定使用值来填充 NaN 值，然后带入计算过程，如下所示："
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "35a7fac3853e31a6"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "      A     B    C     D     E\na   0.0   1.0  2.0   NaN   NaN\nb   3.0   5.0  5.0   2.0   3.0\nc  10.0  12.0  8.0   6.0   7.0\nd   8.0   9.0  NaN  10.0  11.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n      <th>E</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>2.0</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>3.0</td>\n      <td>5.0</td>\n      <td>5.0</td>\n      <td>2.0</td>\n      <td>3.0</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>10.0</td>\n      <td>12.0</td>\n      <td>8.0</td>\n      <td>6.0</td>\n      <td>7.0</td>\n    </tr>\n    <tr>\n      <th>d</th>\n      <td>8.0</td>\n      <td>9.0</td>\n      <td>NaN</td>\n      <td>10.0</td>\n      <td>11.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.add(df2, fill_value=0)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T11:58:00.472951300Z",
     "start_time": "2025-01-08T11:58:00.408443100Z"
    }
   },
   "id": "22294b97cd410877",
   "execution_count": 13
  },
  {
   "cell_type": "markdown",
   "source": [
    "我们指定了使用 0 填充 NaN 值，然后带入计算过程，注意这里填充的不是最终的运算结果。\n",
    "可以看到依然有元素为 NaN 值，这是因为这个位置的元素在 df1 和 df2 中都未定义。\n",
    "\n",
    "大部分的 Series 和 DataFrame 操作中都支持数据自动对齐功能，\n",
    "同时也支持 fill_value 参数指定 NaN 值的填充值。"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "eee9004b145bbdfe"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 常用计算\n",
    "Series DataFrame 的常用运算方式和Numpy中的差不多\n",
    "\n",
    "在 pandas 中还有一种常见的操作是将函数应用到每行或者每一列上面，\n",
    "dataframe的apply方法可以实现此功能，比如想统计每行和每列的极差（max min）"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "2bb6933e9bb48af5"
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "60d5fdb8c31f4b04"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   A  B  C\na  0  1  2\nb  3  4  5\nc  6  7  8",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>3</td>\n      <td>4</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>6</td>\n      <td>7</td>\n      <td>8</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = pd.DataFrame(np.arange(9).reshape(3, 3), \n",
    "                   columns=list('ABC'), index=list('abc'))\n",
    "df1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:01:42.335730700Z",
     "start_time": "2025-01-08T12:01:42.302993100Z"
    }
   },
   "id": "ab1afc64d65aeaf2",
   "execution_count": 14
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "A    6\nB    6\nC    6\ndtype: int64"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def f(x): \n",
    "    return x.max() - x.min()\n",
    "\n",
    "df1.apply(f)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:02:29.642723300Z",
     "start_time": "2025-01-08T12:02:29.592320300Z"
    }
   },
   "id": "4553389af9822123",
   "execution_count": 15
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "a    2\nb    2\nc    2\ndtype: int64"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.apply(f, axis=1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:03:12.666159900Z",
     "start_time": "2025-01-08T12:03:12.621555300Z"
    }
   },
   "id": "9c735dabddf89341",
   "execution_count": 16
  },
  {
   "cell_type": "markdown",
   "source": [
    "以上代码中，我们定义了 f 匿名函数，该匿名函数简单的返回列表的极差，\n",
    "然后首先通过 df.apply(f) 应用 f, 统计出了每列的极差，\n",
    "接着通过传入 axis=1 参数，统计出了每行的极差，\n",
    "\n",
    "如果想将某函数应用到每一个元素上，\n",
    "对于 DataFrame 数据可使用 df.applymap 方法，\n",
    "而对于 Series 数据可以使用 s.map 方法："
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "f2b7155d90719de"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\bzj27\\AppData\\Local\\Temp\\ipykernel_64316\\2823257610.py:1: FutureWarning: DataFrame.applymap has been deprecated. Use DataFrame.map instead.\n",
      "  df1.applymap(lambda x: x+1)\n"
     ]
    },
    {
     "data": {
      "text/plain": "   A  B  C\na  1  2  3\nb  4  5  6\nc  7  8  9",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>1</td>\n      <td>2</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>4</td>\n      <td>5</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>7</td>\n      <td>8</td>\n      <td>9</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.applymap(lambda x: x+1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:04:23.975407400Z",
     "start_time": "2025-01-08T12:04:23.933430600Z"
    }
   },
   "id": "734bb30abb561425",
   "execution_count": 17
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 常用统计\n",
    "\n",
    "类似于 NumPy，Series 和 DataFrame 也有各种统计方法，\n",
    "比如求平均值，方差，和等方法，\n",
    "同时通过 describe 方法可以得知当前数据的一些常用统计信息："
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "67743d3ebc115487"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   A  B  C\na  0  1  2\nb  3  4  5\nc  6  7  8",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>3</td>\n      <td>4</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>6</td>\n      <td>7</td>\n      <td>8</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = pd.DataFrame(np.arange(9).reshape(3, 3), \n",
    "                   columns=list('ABC'), index=list('abc'))\n",
    "df1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:05:42.171923Z",
     "start_time": "2025-01-08T12:05:42.129250300Z"
    }
   },
   "id": "40b533579224273c",
   "execution_count": 18
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "A     9\nB    12\nC    15\ndtype: int64"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:05:59.638148600Z",
     "start_time": "2025-01-08T12:05:59.573710400Z"
    }
   },
   "id": "8a48c229a8152ed3",
   "execution_count": 19
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "a     3\nb    12\nc    21\ndtype: int64"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.sum(axis=1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:06:27.105694400Z",
     "start_time": "2025-01-08T12:06:27.057549900Z"
    }
   },
   "id": "1801182092965779",
   "execution_count": 20
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "A    3.0\nB    4.0\nC    5.0\ndtype: float64"
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.mean()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:06:39.331104100Z",
     "start_time": "2025-01-08T12:06:39.276984800Z"
    }
   },
   "id": "fb57cdd077f45f3d",
   "execution_count": 21
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "a    1.0\nb    4.0\nc    7.0\ndtype: float64"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.mean(axis=1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:07:06.854522500Z",
     "start_time": "2025-01-08T12:07:06.796739800Z"
    }
   },
   "id": "fa1cba12932060ee",
   "execution_count": 22
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "         A    B    C\ncount  3.0  3.0  3.0\nmean   3.0  4.0  5.0\nstd    3.0  3.0  3.0\nmin    0.0  1.0  2.0\n25%    1.5  2.5  3.5\n50%    3.0  4.0  5.0\n75%    4.5  5.5  6.5\nmax    6.0  7.0  8.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>count</th>\n      <td>3.0</td>\n      <td>3.0</td>\n      <td>3.0</td>\n    </tr>\n    <tr>\n      <th>mean</th>\n      <td>3.0</td>\n      <td>4.0</td>\n      <td>5.0</td>\n    </tr>\n    <tr>\n      <th>std</th>\n      <td>3.0</td>\n      <td>3.0</td>\n      <td>3.0</td>\n    </tr>\n    <tr>\n      <th>min</th>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>2.0</td>\n    </tr>\n    <tr>\n      <th>25%</th>\n      <td>1.5</td>\n      <td>2.5</td>\n      <td>3.5</td>\n    </tr>\n    <tr>\n      <th>50%</th>\n      <td>3.0</td>\n      <td>4.0</td>\n      <td>5.0</td>\n    </tr>\n    <tr>\n      <th>75%</th>\n      <td>4.5</td>\n      <td>5.5</td>\n      <td>6.5</td>\n    </tr>\n    <tr>\n      <th>max</th>\n      <td>6.0</td>\n      <td>7.0</td>\n      <td>8.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.describe()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:07:13.480523100Z",
     "start_time": "2025-01-08T12:07:13.433831800Z"
    }
   },
   "id": "ab627c0226531a47",
   "execution_count": 23
  },
  {
   "cell_type": "markdown",
   "source": [
    "可以看到，对于这些统计函数，我们可以指定统计的纬度，和NumPy多维数组的统计函数十分相似。\n",
    "默认情况下统计函数按列统计 DataFrame 的信息，\n",
    "但是可以通过参数 axis=1 指定按行统计相关信息。 \n",
    "\n",
    "describe 方法显示了一些常用的统计信息，具体解释如下：\n",
    "1. count 元素值的数量；\n",
    "2. mean 平均值；\n",
    "3. std 标准差；\n",
    "4. min 最小值；\n",
    "5. 25% 下四分位数；\n",
    "6. 50% 中位数；\n",
    "7. 75% 上四分位数；\n",
    "8. max 最大值；"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "e1624a877b36f73e"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 数据合并和分组\n",
    "\n",
    "有的时候需要合并两个 DataFrame 数据，合并数据的方式主要有两种，\n",
    "一种简单的进行拼接，\n",
    "另一种是根据列名类像数据库表查询一样进行合并。\n",
    "\n",
    "这两种操作可以分别通过调用 pandas.concat 和 pandas.merge 方法实现，演示代码如下："
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "7112551518c7707"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "          0         1         2\n0 -0.652826 -0.872941  1.115167\n1  2.343941 -1.146828  0.489969\n2  0.226786  0.275527 -0.977276",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>0</th>\n      <th>1</th>\n      <th>2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>-0.652826</td>\n      <td>-0.872941</td>\n      <td>1.115167</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2.343941</td>\n      <td>-1.146828</td>\n      <td>0.489969</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.226786</td>\n      <td>0.275527</td>\n      <td>-0.977276</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = pd.DataFrame(np.random.randn(3, 3))\n",
    "df1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:10:01.716080100Z",
     "start_time": "2025-01-08T12:10:01.678121900Z"
    }
   },
   "id": "a5c1d60e092a83ce",
   "execution_count": 25
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "          0         1         2\n5 -1.986168  0.452092  1.136531\n6  1.895293  0.701443 -1.007384\n7 -0.321799  0.368636  0.460384",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>0</th>\n      <th>1</th>\n      <th>2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>5</th>\n      <td>-1.986168</td>\n      <td>0.452092</td>\n      <td>1.136531</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>1.895293</td>\n      <td>0.701443</td>\n      <td>-1.007384</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>-0.321799</td>\n      <td>0.368636</td>\n      <td>0.460384</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = pd.DataFrame(np.random.randn(3, 3),index=[5,6,7])\n",
    "df2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:10:31.686653400Z",
     "start_time": "2025-01-08T12:10:31.628424700Z"
    }
   },
   "id": "c721c3279417b1ef",
   "execution_count": 26
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "          0         1         2\n0 -0.652826 -0.872941  1.115167\n1  2.343941 -1.146828  0.489969\n2  0.226786  0.275527 -0.977276\n5 -1.986168  0.452092  1.136531\n6  1.895293  0.701443 -1.007384\n7 -0.321799  0.368636  0.460384",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>0</th>\n      <th>1</th>\n      <th>2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>-0.652826</td>\n      <td>-0.872941</td>\n      <td>1.115167</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2.343941</td>\n      <td>-1.146828</td>\n      <td>0.489969</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.226786</td>\n      <td>0.275527</td>\n      <td>-0.977276</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>-1.986168</td>\n      <td>0.452092</td>\n      <td>1.136531</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>1.895293</td>\n      <td>0.701443</td>\n      <td>-1.007384</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>-0.321799</td>\n      <td>0.368636</td>\n      <td>0.460384</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat([df1, df2])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:10:50.818317Z",
     "start_time": "2025-01-08T12:10:50.784797400Z"
    }
   },
   "id": "e73ba89faad6a11c",
   "execution_count": 27
  },
  {
   "cell_type": "markdown",
   "source": [
    "上面的代码中，我们使用 pandas.concat 函数成功的拼接了两个 DataFrame 数据。\n",
    "有时候，我们有多个数据集，这些数据集有相同的列，这个时候就可以按照这个列进行合并操作，类似于数据库中的 join 操作："
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "1b3b3d20bf1726cc"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  course  minutes\n0     5348      12        9\n1       13      45       36",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>course</th>\n      <th>minutes</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>5348</td>\n      <td>12</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>13</td>\n      <td>45</td>\n      <td>36</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = pd.DataFrame({\n",
    "    'user_id':[5348,13], \n",
    "    'course':[12,45], \n",
    "    'minutes':[9,36]})\n",
    "df1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:12:30.588109400Z",
     "start_time": "2025-01-08T12:12:30.541992300Z"
    }
   },
   "id": "48e328a0310b484f",
   "execution_count": 28
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   course        name\n0      12  Linux 基础入门\n1      45        数据分析",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>course</th>\n      <th>name</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>12</td>\n      <td>Linux 基础入门</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>45</td>\n      <td>数据分析</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = pd.DataFrame({\n",
    "    'course': [12, 45], \n",
    "    'name': ['Linux 基础入门', '数据分析']})\n",
    "df2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:12:50.066290300Z",
     "start_time": "2025-01-08T12:12:50.009863200Z"
    }
   },
   "id": "25b03533fd72b587",
   "execution_count": 29
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  course  minutes        name\n0     5348      12        9  Linux 基础入门\n1       13      45       36        数据分析",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>course</th>\n      <th>minutes</th>\n      <th>name</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>5348</td>\n      <td>12</td>\n      <td>9</td>\n      <td>Linux 基础入门</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>13</td>\n      <td>45</td>\n      <td>36</td>\n      <td>数据分析</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(df1, df2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:13:02.136071500Z",
     "start_time": "2025-01-08T12:13:02.080421Z"
    }
   },
   "id": "4a9d6e05a4d4f9d0",
   "execution_count": 30
  },
  {
   "cell_type": "markdown",
   "source": [
    "上面的代码中我们通过字典创建了两个数据集，\n",
    "可以看到当通过字段创建 DataFrame 数据集的时，键名变成了列名。\n",
    "\n",
    "df1和df2有共同的列course, 当进行 merge 操作的时候Pandas会自动安装这列进行合并。\n",
    "合并数据集时，有很多参数可以选择，比如选择在根据哪一列进行合并，合并的方式等，\n",
    "可以通过 help(pd.merge) 查看完整的帮助文档。"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "bd2549be46d33b7f"
  },
  {
   "cell_type": "markdown",
   "source": [
    "在 Pandas 中，\n",
    "也支持类似于数据库查询语句 GROUP BY 的功能，\n",
    "也就是按照某列进行分组，然后再分组上进行一些计算操作，\n",
    "\n",
    "假如我们有如下的数据集，那么如何计算其中 user_id 是 5348 的用户的学习时间呢？"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "8bda652d3659413f"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  course  minutes\n0     5348      12        9\n1       13      45       36\n2     5348      23       45",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>course</th>\n      <th>minutes</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>5348</td>\n      <td>12</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>13</td>\n      <td>45</td>\n      <td>36</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>5348</td>\n      <td>23</td>\n      <td>45</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({\n",
    "    'user_id':[5348,13,5348], \n",
    "    'course':[12,45,23], \n",
    "    'minutes':[9,36,45]\n",
    "})\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:26:24.195513800Z",
     "start_time": "2025-01-08T12:26:24.124596200Z"
    }
   },
   "id": "7fc702c0eae14e38",
   "execution_count": 31
  },
  {
   "cell_type": "markdown",
   "source": [
    "一种办法是筛选出所有 user_id 为 5348 的行，然后进行求和统计，如下:"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "bd57ab39756c1095"
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "fa80f6766bf855c4"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "0     9\n2    45\nName: minutes, dtype: int64"
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[df['user_id'] == 5348]['minutes']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:27:20.108731500Z",
     "start_time": "2025-01-08T12:27:20.053993600Z"
    }
   },
   "id": "8a177b0c19e6a7b9",
   "execution_count": 32
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  course  minutes\n0     5348      12        9\n2     5348      23       45",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>course</th>\n      <th>minutes</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>5348</td>\n      <td>12</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>5348</td>\n      <td>23</td>\n      <td>45</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[df['user_id'] == 5348]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:27:32.722301400Z",
     "start_time": "2025-01-08T12:27:32.673149600Z"
    }
   },
   "id": "289c274324ad01df",
   "execution_count": 33
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "0     9\n2    45\nName: minutes, dtype: int64"
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[df['user_id'] == 5348]['minutes']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:27:41.660650400Z",
     "start_time": "2025-01-08T12:27:41.593464700Z"
    }
   },
   "id": "73c2f00420740812",
   "execution_count": 34
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "np.int64(54)"
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[df['user_id'] == 5348]['minutes'].sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:27:47.838275400Z",
     "start_time": "2025-01-08T12:27:47.760942200Z"
    }
   },
   "id": "c520659e430fe434",
   "execution_count": 35
  },
  {
   "cell_type": "markdown",
   "source": [
    "可以看到我们可以先通过 df[df['user_id'] == 5348] 布尔索引筛选出所有的相关行，\n",
    "然后通过结合列筛选功过滤其他列，只剩下时间统计列 (minutes 所在列为学习时间列），\n",
    "最后通过求和运算统计出了学习时间。"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "636b77a4066bb524"
  },
  {
   "cell_type": "markdown",
   "source": [
    "我们也可以使用类似于数据库的 GROUP BY 功能进行计算，演示代码如下："
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "940fda25205e4ffc"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  minutes\n0     5348        9\n1       13       36\n2     5348       45",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>minutes</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>5348</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>13</td>\n      <td>36</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>5348</td>\n      <td>45</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[['user_id', 'minutes']]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:28:27.714404700Z",
     "start_time": "2025-01-08T12:28:27.664373800Z"
    }
   },
   "id": "4ce1dc7e8f8a8e9b",
   "execution_count": 36
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "         minutes\nuser_id         \n13            36\n5348          54",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>minutes</th>\n    </tr>\n    <tr>\n      <th>user_id</th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>13</th>\n      <td>36</td>\n    </tr>\n    <tr>\n      <th>5348</th>\n      <td>54</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[['user_id', 'minutes']].groupby('user_id').sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:28:46.191405700Z",
     "start_time": "2025-01-08T12:28:46.119059100Z"
    }
   },
   "id": "173bea2d55328021",
   "execution_count": 37
  },
  {
   "cell_type": "markdown",
   "source": [
    "可以看到，先过滤列，只剩下了 user_id 和 minutes 列，\n",
    "然后通过 groupby 方法在 user_id 列上进行分组并求和，\n",
    "相对于前一种方式，分组求和更加灵活一些"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "926d96d5519619fe"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "         course  minutes\nuser_id                 \n13           45       36\n5348         35       54",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>course</th>\n      <th>minutes</th>\n    </tr>\n    <tr>\n      <th>user_id</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>13</th>\n      <td>45</td>\n      <td>36</td>\n    </tr>\n    <tr>\n      <th>5348</th>\n      <td>35</td>\n      <td>54</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby('user_id').sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:29:37.526745Z",
     "start_time": "2025-01-08T12:29:37.461805800Z"
    }
   },
   "id": "7440846f2e5c30fe",
   "execution_count": 38
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 时间序列处理\n",
    "\n",
    "在 Python 中，时间处理方面的相关功能主要集中在 datetime 包中，主要有的三种对象：\n",
    "\n",
    "1. datetime.datetime 代表时间对象；\n",
    "2. datetime.date 代表某一天；\n",
    "3. datetime.timedelta 代表两个 datetime.datetime 对象之间的时间差；"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "bffcb78d98057eb8"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "datetime.datetime(2017, 8, 1, 0, 0)"
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from datetime import datetime, timedelta\n",
    "\n",
    "d1 = datetime(2017,8,1)\n",
    "d1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:31:18.358660600Z",
     "start_time": "2025-01-08T12:31:18.337740Z"
    }
   },
   "id": "6df602e62a11dce5",
   "execution_count": 40
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "datetime.datetime(2017, 8, 11, 0, 0)"
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "delta = timedelta(days=10)\n",
    "d1 + delta"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:31:49.414193300Z",
     "start_time": "2025-01-08T12:31:49.376277Z"
    }
   },
   "id": "1a458a661f824106",
   "execution_count": 41
  },
  {
   "cell_type": "markdown",
   "source": [
    "演示基本用法 "
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "769af6d7ebbcc325"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "2018-01-01    1.307103\n2018-01-02   -0.206019\n2018-01-03   -0.627721\n2018-01-04    1.518301\ndtype: float64"
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "dates = [datetime(2018,1,1), datetime(2018,1,2),\n",
    "            datetime(2018,1,3), datetime(2018,1,4)]\n",
    "\n",
    "ts = pd.Series(np.random.randn(4), index=dates)\n",
    "ts"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:34:13.109169400Z",
     "start_time": "2025-01-08T12:34:13.058065400Z"
    }
   },
   "id": "916de97a9b76688b",
   "execution_count": 42
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04'], dtype='datetime64[ns]', freq=None)"
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.index"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:34:40.080906400Z",
     "start_time": "2025-01-08T12:34:40.009117500Z"
    }
   },
   "id": "1087fd91247e172e",
   "execution_count": 43
  },
  {
   "cell_type": "markdown",
   "source": [
    "以上代码中，我们通过 datetime 时间类型生成了一堆时间，\n",
    "然后基于此创建了一个时间序列 ts , \n",
    "可以看到 ts 的索引类型为 DatetimeIndex 类型。\n",
    "\n",
    "生成 ts 以后我们就有多种方法选择元素了，\n",
    "只需要传入一个能被 Pandas 识别的日期字符串就可以了："
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "aca5771cf1f197b"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "Timestamp('2018-01-01 00:00:00')"
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.index[0]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:34:52.203619200Z",
     "start_time": "2025-01-08T12:34:52.154633100Z"
    }
   },
   "id": "d798087d1a1f9849",
   "execution_count": 44
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "np.float64(1.3071029044367382)"
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts[ts.index[0]]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:35:08.347822700Z",
     "start_time": "2025-01-08T12:35:08.284355100Z"
    }
   },
   "id": "120eab720ccefd51",
   "execution_count": 45
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "np.float64(1.3071029044367382)"
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts['2018-01-01']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:35:21.158120700Z",
     "start_time": "2025-01-08T12:35:21.109754600Z"
    }
   },
   "id": "f27872ed0205c557",
   "execution_count": 46
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "np.float64(1.3071029044367382)"
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts['2018/01/01']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:35:25.995357300Z",
     "start_time": "2025-01-08T12:35:25.948299700Z"
    }
   },
   "id": "fa403628b77f803e",
   "execution_count": 47
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "np.float64(1.3071029044367382)"
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts['1/1/2018']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:35:30.463644900Z",
     "start_time": "2025-01-08T12:35:30.342633200Z"
    }
   },
   "id": "52e2f9e2b5ba3a60",
   "execution_count": 48
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "np.float64(1.3071029044367382)"
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts[datetime(2018, 1, 1)]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:36:07.129103200Z",
     "start_time": "2025-01-08T12:36:07.070620300Z"
    }
   },
   "id": "40ee7cdd84567393",
   "execution_count": 49
  },
  {
   "cell_type": "markdown",
   "source": [
    "可以看到选择元素的方式非常灵活。\n",
    "\n",
    "在 Pandas 中生成日期范围也非常灵活，\n",
    "主要通过 pandas.date_range 函数完成，该函数主要有以下几个参数：\n",
    "1. start: 指定了日期范围的起始时间；\n",
    "2. end: 指定了日期范围的结束时间；\n",
    "3. periods: 指定了间隔范围，如果只是指定start和end日期的其中一个，则需要改参数；\n",
    "4. freq: 指定了日期频率，\n",
    "    比如 D 代表每天，H 代表每小时，M 代表月，\n",
    "    这些频率字符前也可以指定一个整数，代表具体多少天，多少小时，比如 5D 代表 5 天。\n",
    "    还有一些其他的频率字符串，\n",
    "    比如 MS 代表每月第一天，BM 代表每月最后一个工作日，\n",
    "    或者是频率组合字符串，比如 1h30min 代表 1 小时 30 分钟。"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "80dda23dbd044fcc"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\bzj27\\AppData\\Local\\Temp\\ipykernel_64316\\2335527799.py:1: FutureWarning: 'M' is deprecated and will be removed in a future version, please use 'ME' instead.\n",
      "  pd.date_range('2018-1-1','2019',freq='M')\n"
     ]
    },
    {
     "data": {
      "text/plain": "DatetimeIndex(['2018-01-31', '2018-02-28', '2018-03-31', '2018-04-30',\n               '2018-05-31', '2018-06-30', '2018-07-31', '2018-08-31',\n               '2018-09-30', '2018-10-31', '2018-11-30', '2018-12-31'],\n              dtype='datetime64[ns]', freq='ME')"
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range('2018-1-1','2019',freq='M')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:38:40.791581500Z",
     "start_time": "2025-01-08T12:38:40.745799300Z"
    }
   },
   "id": "a31fc6f7b61e8daf",
   "execution_count": 52
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "DatetimeIndex(['2018-01-01', '2018-02-01', '2018-03-01', '2018-04-01',\n               '2018-05-01', '2018-06-01', '2018-07-01', '2018-08-01',\n               '2018-09-01', '2018-10-01', '2018-11-01', '2018-12-01'],\n              dtype='datetime64[ns]', freq='MS')"
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range('2018-1-1', '2018-12-1', freq='MS')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:39:42.116658200Z",
     "start_time": "2025-01-08T12:39:42.063772600Z"
    }
   },
   "id": "a624667847c13ac",
   "execution_count": 53
  },
  {
   "cell_type": "markdown",
   "source": [
    "freq 参数还有许多频率指定的方式，篇幅有限就不一一介绍了。\n",
    "\n",
    "有的时候时间序列是按每小时显示统计的，但是我们想将统计频率转换成按天来统计，\n",
    "这个时候可以使用时间序列的 resample 方法，\n",
    "该方法非常强大，不仅仅支持高频率的数据聚合到低频率 （降采样），\n",
    "也支持低频率转化到高频率统计（升采样），先创建数据集"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "9a65a1e45b284413"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\bzj27\\AppData\\Local\\Temp\\ipykernel_64316\\992013664.py:1: FutureWarning: 'H' is deprecated and will be removed in a future version, please use 'h' instead.\n",
      "  dates = pd.date_range('2018-1-1', '2018-1-2 23:00:00', freq='H')\n"
     ]
    },
    {
     "data": {
      "text/plain": "2018-01-01 00:00:00     0\n2018-01-01 01:00:00     1\n2018-01-01 02:00:00     2\n2018-01-01 03:00:00     3\n2018-01-01 04:00:00     4\n2018-01-01 05:00:00     5\n2018-01-01 06:00:00     6\n2018-01-01 07:00:00     7\n2018-01-01 08:00:00     8\n2018-01-01 09:00:00     9\n2018-01-01 10:00:00    10\n2018-01-01 11:00:00    11\n2018-01-01 12:00:00    12\n2018-01-01 13:00:00    13\n2018-01-01 14:00:00    14\n2018-01-01 15:00:00    15\n2018-01-01 16:00:00    16\n2018-01-01 17:00:00    17\n2018-01-01 18:00:00    18\n2018-01-01 19:00:00    19\n2018-01-01 20:00:00    20\n2018-01-01 21:00:00    21\n2018-01-01 22:00:00    22\n2018-01-01 23:00:00    23\n2018-01-02 00:00:00    24\n2018-01-02 01:00:00    25\n2018-01-02 02:00:00    26\n2018-01-02 03:00:00    27\n2018-01-02 04:00:00    28\n2018-01-02 05:00:00    29\n2018-01-02 06:00:00    30\n2018-01-02 07:00:00    31\n2018-01-02 08:00:00    32\n2018-01-02 09:00:00    33\n2018-01-02 10:00:00    34\n2018-01-02 11:00:00    35\n2018-01-02 12:00:00    36\n2018-01-02 13:00:00    37\n2018-01-02 14:00:00    38\n2018-01-02 15:00:00    39\n2018-01-02 16:00:00    40\n2018-01-02 17:00:00    41\n2018-01-02 18:00:00    42\n2018-01-02 19:00:00    43\n2018-01-02 20:00:00    44\n2018-01-02 21:00:00    45\n2018-01-02 22:00:00    46\n2018-01-02 23:00:00    47\nFreq: h, dtype: int64"
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dates = pd.date_range('2018-1-1', '2018-1-2 23:00:00', freq='H')\n",
    "\n",
    "ts = pd.Series(np.arange(len(dates)), index=dates)\n",
    "ts"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:43:38.479657700Z",
     "start_time": "2025-01-08T12:43:38.396286100Z"
    }
   },
   "id": "d9318201e3698ffc",
   "execution_count": 57
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "48"
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.size"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:43:48.518796200Z",
     "start_time": "2025-01-08T12:43:48.464246300Z"
    }
   },
   "id": "41acb6954ca02297",
   "execution_count": 58
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "2018-01-01 00:00:00    0\n2018-01-01 01:00:00    1\n2018-01-01 02:00:00    2\n2018-01-01 03:00:00    3\n2018-01-01 04:00:00    4\nFreq: h, dtype: int64"
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.head(5)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:43:57.723901700Z",
     "start_time": "2025-01-08T12:43:57.662558200Z"
    }
   },
   "id": "b6a5664585831898",
   "execution_count": 59
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "2018-01-02 19:00:00    43\n2018-01-02 20:00:00    44\n2018-01-02 21:00:00    45\n2018-01-02 22:00:00    46\n2018-01-02 23:00:00    47\nFreq: h, dtype: int64"
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.tail(5)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:44:06.743937600Z",
     "start_time": "2025-01-08T12:44:06.606237300Z"
    }
   },
   "id": "e57dc9fcf0a5aacd",
   "execution_count": 60
  },
  {
   "cell_type": "markdown",
   "source": [
    "resample('D')方法指定了按天统计\n",
    "接着使用sum方法指定了最终数据是按天的所有数据的和进行统计"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "40d94504985d046b"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "2018-01-01    276\n2018-01-02    852\nFreq: D, dtype: int64"
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.resample('D').sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:44:24.503134200Z",
     "start_time": "2025-01-08T12:44:24.451258300Z"
    }
   },
   "id": "f2ac3c2e9fe62d49",
   "execution_count": 61
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "2018-01-01    11.5\n2018-01-02    35.5\nFreq: D, dtype: float64"
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.resample('D').mean()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:45:28.902700100Z",
     "start_time": "2025-01-08T12:45:28.841045500Z"
    }
   },
   "id": "5f61daf971cca2d3",
   "execution_count": 62
  },
  {
   "cell_type": "markdown",
   "source": [
    "当把低频率的数据转换成高频率的数据时，\n",
    "默认情况下 Pandas 会引入 NaN 值，\n",
    "因为没办法从低频率的数据计算出高频率的数据，\n",
    "但可以通过 fill_method 参数指定插值方式："
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "834cd39aa6cd935"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\bzj27\\AppData\\Local\\Temp\\ipykernel_64316\\4283683540.py:1: FutureWarning: 'H' is deprecated and will be removed in a future version, please use 'h' instead.\n",
      "  ts.resample('D').mean().resample('H').mean()\n"
     ]
    },
    {
     "data": {
      "text/plain": "2018-01-01 00:00:00    11.5\n2018-01-01 01:00:00     NaN\n2018-01-01 02:00:00     NaN\n2018-01-01 03:00:00     NaN\n2018-01-01 04:00:00     NaN\n2018-01-01 05:00:00     NaN\n2018-01-01 06:00:00     NaN\n2018-01-01 07:00:00     NaN\n2018-01-01 08:00:00     NaN\n2018-01-01 09:00:00     NaN\n2018-01-01 10:00:00     NaN\n2018-01-01 11:00:00     NaN\n2018-01-01 12:00:00     NaN\n2018-01-01 13:00:00     NaN\n2018-01-01 14:00:00     NaN\n2018-01-01 15:00:00     NaN\n2018-01-01 16:00:00     NaN\n2018-01-01 17:00:00     NaN\n2018-01-01 18:00:00     NaN\n2018-01-01 19:00:00     NaN\n2018-01-01 20:00:00     NaN\n2018-01-01 21:00:00     NaN\n2018-01-01 22:00:00     NaN\n2018-01-01 23:00:00     NaN\n2018-01-02 00:00:00    35.5\nFreq: h, dtype: float64"
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.resample('D').mean().resample('H').mean()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:45:36.260469600Z",
     "start_time": "2025-01-08T12:45:36.193902900Z"
    }
   },
   "id": "4474adbf8e03fef5",
   "execution_count": 63
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\bzj27\\AppData\\Local\\Temp\\ipykernel_64316\\3725415122.py:1: FutureWarning: 'H' is deprecated and will be removed in a future version, please use 'h' instead.\n",
      "  ts.resample('D').mean().resample('H').ffill()\n"
     ]
    },
    {
     "data": {
      "text/plain": "2018-01-01 00:00:00    11.5\n2018-01-01 01:00:00    11.5\n2018-01-01 02:00:00    11.5\n2018-01-01 03:00:00    11.5\n2018-01-01 04:00:00    11.5\n2018-01-01 05:00:00    11.5\n2018-01-01 06:00:00    11.5\n2018-01-01 07:00:00    11.5\n2018-01-01 08:00:00    11.5\n2018-01-01 09:00:00    11.5\n2018-01-01 10:00:00    11.5\n2018-01-01 11:00:00    11.5\n2018-01-01 12:00:00    11.5\n2018-01-01 13:00:00    11.5\n2018-01-01 14:00:00    11.5\n2018-01-01 15:00:00    11.5\n2018-01-01 16:00:00    11.5\n2018-01-01 17:00:00    11.5\n2018-01-01 18:00:00    11.5\n2018-01-01 19:00:00    11.5\n2018-01-01 20:00:00    11.5\n2018-01-01 21:00:00    11.5\n2018-01-01 22:00:00    11.5\n2018-01-01 23:00:00    11.5\n2018-01-02 00:00:00    35.5\nFreq: h, dtype: float64"
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.resample('D').mean().resample('H').ffill()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:45:51.257070500Z",
     "start_time": "2025-01-08T12:45:51.192583600Z"
    }
   },
   "id": "3724411391217210",
   "execution_count": 64
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "e1c7b976b29f1415"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
